<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
/**
 * 分销相关
 */

namespace app\services\car;

use app\services\agent\AgentLevelServices;
use app\services\BaseServices;
use app\services\user\UserBrokerageServices;
use app\services\user\UserServices;

class CommissionServices extends BaseServices{

    /**
     * 一级返佣
     * @param $orderInfo
     * @param $userInfo
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function backOrderBrokerage($orderInfo, $userInfo)
    {
        // 当前订单｜用户不存在  直接返回
        if (!$orderInfo || !$userInfo) return true;
        //商城分销功能是否开启 0关闭1开启
        if (!sys_config('brokerage_func_status')) return true;
        //是否开启自购返佣
        $isSelfBrokerage = sys_config('is_self_brokerage', 0);
        //没开启自购返佣 没有上级 或者 当用用户上级时自己  直接返回
        if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) return true;
        $one_spread_uid = $userInfo['spread_uid'];
        //检测是否是分销员
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        //获取后台一级返佣比例
        /** @var AgentLevelServices $agentLevelServices */
        $agentLevelServices = app()->make(AgentLevelServices::class);
        [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid] = $agentLevelServices->getAgentLevelBrokerage($orderInfo['uid']);
        $BrokerageOne = sys_config('store_brokerage_ratio') != '' ? sys_config('store_brokerage_ratio') : 0;
        $storeBrokerageRatio = $BrokerageOne + (($BrokerageOne * $one_brokerage_up) / 100);//一级比例
        //一级返佣比例 小于等于零时直接返回 不返佣
        if ($storeBrokerageRatio <= 0) return true;
        //计算获取一级返佣比例
        $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
        $brokeragePrice = bcmul($orderInfo['type_price'], $brokerageRatio, 2);
        if ($brokeragePrice <= 0) return true;
        // 获取上级推广员信息
        $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
        // 上级推广员返佣之后的金额
        $balance = bcadd($spreadPrice, $brokeragePrice, 2);
        // 添加用户佣金
        $res1 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
        if ($res1) {
            //冻结时间
            $broken_time = intval(sys_config('extract_time'));
            $frozen_time = time() + $broken_time * 86400;
            // 添加佣金记录
            /** @var UserBrokerageServices $userBrokerageServices */
            $userBrokerageServices = app()->make(UserBrokerageServices::class);
            //自购返佣 ｜｜ 上级
            $type = $one_spread_uid == $orderInfo['uid'] ? 'get_self_car_brokerage' : 'get_car_brokerage';
            $res = $userBrokerageServices->income($type, $one_spread_uid, [
                'nickname' => $userInfo['nickname'],
                'pay_price' => floatval($orderInfo['type_price']),
                'number' => floatval($brokeragePrice),
                'frozen_time' => $frozen_time
            ], $balance, $orderInfo['id']);
        }
        return $res1;
    }

}
